<template>
    <el-upload multiple :action="`/api/shop/admin${action}`" accept=".xls,.xlsx,csv" :headers="headers" :on-success="handleSuccess" :on-progress="handleProgress" :show-file-list="false">
        <el-button :loading="loading" icon="el-icon-upload2" type="primary">批量导入</el-button>
    </el-upload>
</template>
<script>
//获取cookie
function getCookie (c_name) {
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            var c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
export default {
    props: {
        action: {
            type: String,
            default: ''
        }
    },
    data () {
        return {
            headers: {},
            loading: false
        };
    },
    mounted () {
        if (getCookie("authorization")) {
            this.headers["authorization"] = getCookie("authorization");
        }
        if (getCookie("appId")) {
            this.headers["appId"] = getCookie("appId");
        }
    },
    methods: {
        handleProgress () {
            this.loading = true
        },
        handleSuccess () {
            this.loading = false
            this.$emit('change')
        }
    }
};
</script>
